package Database;

import java.sql.*;

import Helpers.DBMethods;

public class ConnectDB {

	public static final String DATABASE = "Voting";

	public static final String USER = "root";

	public static final String PASSWORD = "Alpha10)!";
	
	public static final String PORT = "3306";

	public static final String CONNECTIONSTRING = "jdbc:mysql://localhost:"+PORT+"/"
			+ DATABASE + "?" + "user=" + USER + "&password=" + PASSWORD;

	private static Connection conn;

	public static Connection createConnection() {
		try {
			Class.forName("org.gjt.mm.mysql.Driver").newInstance();
			conn = DriverManager.getConnection(CONNECTIONSTRING);
			conn.setAutoCommit(false);
			return conn;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static ResultSet executeQuery(String query) {
		try {
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(query);
			return rs;
		} catch (SQLException e) {
			e.printStackTrace();			
			return null;
		}
	}

	public static int executeNonQuery(String command) {
		int rowsAffected = _executeNonQuery(command);
		return rowsAffected;
	}

	private static int _executeNonQuery(String command) {
		try {
			Statement stmt = conn.createStatement();
			int rowsAffected = stmt.executeUpdate(command);
			return rowsAffected;
		} catch (Exception e) {
			e.printStackTrace();			
			return -1;
		}
	}
	
	public static void main(String [] args)
	{
		DBMethods dbm = new DBMethods();
		System.out.println(dbm.getStudent("karim.elshahed", " "));
	}
}
